package com.jspxcms.plug.repository.impl;

import com.jspxcms.common.orm.JpqlBuilder;
import com.jspxcms.common.orm.Limitable;
import com.jspxcms.plug.domain.PlugDoctor;
import com.jspxcms.plug.domain.Resume;
import com.jspxcms.plug.repository.PlugDoctorDaoPlus;
import com.jspxcms.plug.repository.ResumeDaoPlus;
import org.apache.commons.lang3.ArrayUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.List;

public class PlugDoctorDaoImpl implements PlugDoctorDaoPlus {
    @SuppressWarnings("unchecked")
    public List<PlugDoctor> getList(Integer[] siteId, Limitable limitable) {
        JpqlBuilder jpql = new JpqlBuilder();
        jpql.append("from Resume bean where 1=1");
        if (ArrayUtils.isNotEmpty(siteId)) {
            jpql.append(" and bean.site.id in (:siteId)");
            jpql.setParameter("siteId", Arrays.asList(siteId));
        }
        return jpql.list(em, limitable);
    }

    @Override
    public List findByDepartmentId(Integer departmentId) {
        Query query = em.createNativeQuery("select doctor.* , disease.* \n" +
                "from plug_doctor doctor\n" +
                "join plug_doctor_disease doctordisease on doctor.f_doctor_id =  doctordisease.f_doctor_id\n" +
                "join plug_disease disease on doctordisease.f_disease_id = disease.f_disease_id\n" +
                "where doctor.f_department_id = " + departmentId);
        List rows = query.getResultList();
        return rows;
    }

    private EntityManager em;

    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }
}
